Macromedia Pollster
Part II. How the Pollster Administration Interface Works
Macromedia Pollster is not only a useful tool in itself but it also serves as an excellent application architecture model. Besides being based heavily on components, it demonstrates such principles as error handling, data persistence, and the division of presentation and application business logic.
The best way to understand Pollster's architecture is to realize that we built it from the ground up. When you build applications this way you typically progress through the following steps:
- Gather the requirements.
- Model the data.
- Manipulate the data.
- Design and implement the user interface.
Designing and building your user interface last may seem like an unusual approach to application development, however it actually results in a very powerful, very flexible application architecture.
Step 1. Gathering the Requirements
Before you can start building any application, you have to know what you are building and what your application generally is going to do. After we decided that we were going to build a polling application, we decided on the following set of requirements:
- Each poll only needs one question. Our goal was not to tackle an entire survey application but to provide enough functionality to help developers easily build a single-question poll and get it deployed in a matter of minutes.
- Each individual poll should support up to five options.
- Polls must be able to ignore multiple responses from a single client. Otherwise, a single user could completely skew your results.
- Developers must be easily able to activate and deactivate polls. When a poll is deactivated, it does not allow responses to be submitted.
Note that we gave absolutely no consideration to what the user interface would look like or how it would work. Focusing too much on user interaction at this point in the development process is distracting and prevents you from focusing on your core requirements.
Step 2. Modeling the Data
Now that we had a general set of application requirements, we were able to work out an appropriate data model. At this point in any development process, we like to think in terms of nouns and adjectives. Applications have models (nouns) and those models have attributes (adjectives). Eventually those nouns do things (verbs) but while we're engaged in the data modeling step in the development process, it's important not to worry about verbs prematurely.
The fist question we asked ourselves is which nouns we needed to model. In the case of Pollster, the answers were pretty clear: polls, options, and responses. Because there is often a one-to-one mapping between models and database tables, we decided that the Pollster database should contain tables called Polls, Options, and Responses.
Next we considered the types of properties our models would need in order for us to implement our requirements properly. Another way to look at this is to ask what each model has or needs. Polls contain names, questions, the ability to restrict client responses, the ability to be active or inactive, and one or more options.
Each of these properties translates into columns in the Polls table:
- pollId (primary key)
- name (varchar)
- question (varchar)
- limitByUserHash (Boolean)
- active (Boolean)
- creationDate (date)
In order to model the concept of polls containing options, we added a reference back to the Polls table when we designed the Options table with these columns:
- optionId (primary key)
- pollId (foreign key)
- optionText (varchar)
- sortOrder (small int)
Finally, the Responses table has the following columns:
- optionId (primary key)
- ipAddress (string)
- responseDate (date)
Now that we had our nouns and adjectives in place, we started thinking in terms of verbs.
Step 3. Manipulating the Data
Whenever you have a set of models in an application, it's usually pretty clear what those models are going to do. For instance, we knew we would have to get, save, update, and delete polls, options and responses. We devised an Entities component package (located in pollster/components/entities) and created three components: Polls.cfc, Options.cfc, and Responses.cfc. Each component contains functions for getting, saving, updating, and deleting, and each function contains the necessary SQL code to perform the specific operations. We then tested each component individually and worked out any bugs before writing additional code on top of the components. (Failing to take this step have made finding "low-level" bugs much more difficult.)
Placing all the SQL code for an entire application in a single component package is nice because you can locate it easily and make changes to it quickly and efficiently. If you move to another database, for instance, that supports slightly different syntax, you can make all the necessary changes to a single package of components without having to change the rest of the application at all. For that matter, you could rewrite the entities to interact with text files for data persistence instead of a database and the rest of the application would never know the difference.
One component package was not enough to provide the level of data abstraction we felt the application needed. Although using the Entities components meant we could avoid writing SQL code elsewhere in the application, we still needed more complex operations than the entities provided. For instance, the Poll component could delete a specific poll but couldn't handle the poll's options or the responses to those options. Because of the way we designed the database, in fact, it would not even be possible to delete a poll without first deleting all the poll's responses, then the poll's options, and lastly the poll itself. Additionally, these operations needed to take place within transactions in order to ensure data integrity. We created another package of components, called "Managers," which is designed to manage the relationships between entities. If all you want to do is get data for a specific poll, then using an entity component is fine. For more complex operations like saving or deleting polls, options and responses, higher level functions are much more convenient.
We created two components in the Managers package: PollManager.cfc and ResponseManager.cfc. Each component contains functions for working with multiple entities. For instance, rather than simply having a save function in the PollManager component that only takes in poll data, we created a save function that takes in both poll and option data and works with the Polls and the Options entities (within the safety of a transaction) to insert both sets of data. Once we wrote both managers, we were again able to write a set of tests to isolate the functionality of the Managers components and make sure they worked as expected. At this point, we'd written roughly half the application without writing a single HTML tag.
Although we had a set of high-level, well-tested components for managing poll and poll-related data, there was still one more package of components missing. The best way to understand the next layer of code is to look at an example.
The PollManager component has a savePoll function that takes in all the data that comprises a new poll. The function starts out like this:
<cffunction name="savePoll"
returnType="void"
output="no"
hint="Saves a poll and all a poll's options.">
<cfargument name="name" type="string" required="yes"/>
<cfargument name="question" type="string" required="yes"/>
<cfargument name="limitByUserHash" type="boolean" required="yes"/>
<cfargument name="active" type="boolean" required="yes"/>
<cfargument name="options" type="array" required="yes"/>
The missing piece of architecture is the code that takes data submitted from the user interface, validates it, puts the options in an array, and finally passes all the data into the savePoll function of the PollManager component. To fill this gap, we created a package of components called "Handlers" that handles direct submission from HTML forms. These components validate the data submitted in the form, generate error messages if necessary, call functions on manager or entity components if appropriate, and either redirect back to the page that submitted the data (if the data did not validate) or redirects to the next page in the application. The PollHandler component contains the functions savePoll, deletePoll, and editPoll—the three main operations that the user interface supports. Each function takes variables out of the FORM or URL scopes, performs the appropriate validation, and passes the data along to the appropriate manager components, which, in turn, chain multiple calls to the entity components in order to perform the desired operations. This time we didn't bother writing special code to test the handler components; we decided to test them with the actual application interface.
Step 4. Designing and Manipulating the User Interface
By this time we had built three packages of components, along with a fair amount of test code, without giving much serious thought at all to the user interface. Having well-defined requirements typically lets you to make enough assumptions at each different application layer so that the user interface is really nothing more than one more well-isolated application layer.
We knew our application architecture was sound after we could complete the entire user interface in a short period of time without practically any disruptions or issues. Because half the application code had already been well tested, we were able to focus on laying out the interface rather than trying to track down and fix low-level bugs. When building applications from the bottom up, you typically find that as you get closer to the top—that is, closer to the user interface—every layer of code is easier and faster to write than the layer below it. By the time you get to the user interface, you shouldn't have to write much code at all; you can focus almost exclusively on the user experience. Any code you write should contain presentation logic only, like loops to output data, and the occasional CFIF statement to determine whether a check box should be checked or not. The thinner the presentation layer is in terms of application logic, the better. Although complex presentation logic is certainly fine, if necessary, even that should be well encapsulated in custom tags, components, or include files.
More Thoughts on Bottom-up Application Development
Developing applications strictly from the bottom up is, admittedly, not the fastest way to get your application to the point where you can actually see it working. Developers often resist this approach because they say it takes too long to write four layers of code (entities, managers, handlers, and the user interface) for every application (or even more), in addition to the occasional custom tag and user-defined function. This reluctance is short-sighted, however. When considering how to architect your applications, you should weigh the long-term benefits over the short-term gains.
Although we were unable to get something up and running in a matter of hours, as one usually can when developing from the top down using ColdFusion, by the time we got to the user interface, we were able to concentrate fully on the user experience, knowing that all the layers of code below the user interface were sound and well tested. Additionally, our architecture is easy to extend to maintain. Rather than making large changes to big chunks of code, a multitiered architecture lets you to make little isolated changes to different levels of code, most of which you can individually test. Finally, the amount of code reuse you get out of a multitiered architecture is typically far more extensive than what you get out of flatter designs. Your applications are more robust and easier to maintain.